package com.wolf.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wolf.javabean.API.ApiDatas;
import com.wolf.javabean.API.Apis;
import com.wolf.javabean.ApisBean;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class APIHandle extends ChannelInboundHandlerAdapter {

	public static Map<String,List<ApisBean>> apisMap = new HashMap<String,List<ApisBean>>();
	
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		if(msg instanceof ApiDatas){
			ApiDatas datas = (ApiDatas)msg;
			List<Apis> apisList = datas.getApisList();
			int a = 0;
			
			for(Apis apis : apisList){
				String apiGroup = apis.getApiGroup();
				if(a == 0){
					apisMap.remove(apiGroup);
				}
				if(!apisMap.containsKey(apiGroup)){
					apisMap.put(apiGroup, new ArrayList<ApisBean>());
				}
				ApisBean apisBean = new ApisBean();
				apisBean.setApiGroup(apiGroup);
				apisBean.setMethodAPI(apis.getMethodAPI());
				apisBean.setMethodFieldAPI(apis.getMethodFieldAPI());
				apisBean.setReturnTypeAPI(apis.getReturnTypeAPI());
				apisBean.setReturnMessageAPI(apis.getReturnMessageAPI());
				apisBean.setMethodUseAPI(apis.getMethodUseAPI());
				String[] linesApi = new String[apis.getLinesAPIList().size()];
				apisBean.setLinesAPI(apis.getLinesAPIList().toArray(linesApi));
				
				apisBean.setLockNames(apis.getLockNames());
				apisBean.setSecondnum(apis.getSecondnum());
				apisBean.setGeWait(apis.getGeWait());
				apisBean.setTrans(apis.getTrans());
				apisBean.setTranCommitMinute(apis.getTranCommitMinute());
				apisBean.setTranRollbackMinute(apis.getTranRollbackMinute());
				apisBean.setThreadPoolNumMax(apis.getThreadPoolNumMax());
				apisBean.setHasFile(apis.getHasFile());
				apisBean.setFileIn(apis.getFileIn());
				apisBean.setInterfacetype(apis.getInterfacetype());
				
				apisMap.get(apiGroup).add(apisBean);
				a++;
			}
			
		}
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		super.channelActive(ctx);
	}

	

}
